package com.gavin.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IOUtils {
	private static Logger logger=LoggerFactory.getLogger(IOUtils.class);

	/**
	 * 读取文本文件的内容
	 * 
	 * @param strFile
	 * @param strCoding
	 * @return
	 */
	public static String readTextFile(String strFile) {
		FileInputStream stream = null;
		InputStreamReader reader = null;
		try {
			stream = new java.io.FileInputStream(strFile);
			reader = new InputStreamReader(stream);
			char[] chsData = new char[(int) stream.getChannel().size()];
			int nReadSize = reader.read(chsData, 0, chsData.length);
			return new String(chsData, 0, nReadSize);
		} catch (Exception e) {
			return null;
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				if (stream != null) {
					stream.close();
				}
			} catch (Exception e) {

			}
		}
	}

	/**
	 * 读取文本文件的内容
	 * 
	 * @param strFile
	 * @param strEncoding
	 * @return
	 */
	public static String readTextFile(String strFile, String strEncoding) {
		FileInputStream stream = null;
		InputStreamReader reader = null;
		try {
			stream = new java.io.FileInputStream(strFile);
			reader = new InputStreamReader(stream, Utility.getCharset(strEncoding));
			char[] chsData = new char[(int) stream.getChannel().size()];
			int nReadSize = reader.read(chsData, 0, chsData.length);
			return new String(chsData, 0, nReadSize);
		} catch (Exception e) {
			logger.error("加载文件异常：{}",e.getMessage());
			logger.error("请检查文件路径是否匹配：{}",new File(strFile).getAbsolutePath());
			return null;
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
				if (stream != null) {
					stream.close();
				}
			} catch (Exception e) {

			}
		}
	}

	/**
	 * 读取文本Source的内容
	 * 
	 * @param strFile
	 * @param strEncoding
	 *            默认为utf-8
	 * @return
	 */
	public static String readTextResource(String strFile, String strEncoding, ClassLoader classLoader) {
		if (classLoader == null) {
			classLoader = Utility.class.getClassLoader();
		}
		InputStream stream = null;
		InputStreamReader reader = null;
		try {
			if (classLoader != null) {
				stream = Resources.getResourceAsStream(classLoader, strFile);
			} else {
				stream = Resources.getResourceAsStream(strFile);
			}
			if (strEncoding != null) {
				reader = new InputStreamReader(stream, Utility.getCharset(strEncoding));
			} else {
				reader = new InputStreamReader(stream);
			}

			StringBuilder strbContent = new StringBuilder();
			char[] chsData = new char[1024];
			while (true) {
				int nReadSize = reader.read(chsData, 0, chsData.length);
				if (nReadSize <= 0) {
					break;
				}
				strbContent.append(new String(chsData, 0, nReadSize));
			}
			return strbContent.toString();
		} catch (Exception e) {
			logger.error("加载文件异常：{}",e.getMessage());
			logger.error("请检查文件路径是否匹配：{}",new File(strFile).getAbsolutePath());
			return null;
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
			}
			if (stream != null) {
				try {
					stream.close();
				} catch (Exception e) {

				}
			}
		}
	}
	
	
	// ----------------------------------------------关闭数据流-----------------------------------------------
	public final static void closeStream(InputStream stream) {
		if (stream != null) {
			try {
				stream.close();
			} catch (Exception e) {
			}
		}
	}

	public final static void closeStream(FileOutputStream stream) {
		if (stream != null) {
			try {
				stream.close();
			} catch (Exception e) {
			}
		}
	}
}
